<?php

global $_CONF;

$_CONF['debug'] = false;

App::loadAjax('Account', array
(
	'getUsersOnline',
	'changeImStatus',
	'searchFriends',
	'checkMessages',
	'updateUsersOnline',
	'deleteRequest',
	'acceptRequest',
	'deleteSessions',
	'saveUserSetting'
));

if ( !isset($_POST['xajax']) )
{
	$oDb =& Database::get();
	$oTpl =& App::getTemplate();

	$iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('msg_online') ." WHERE userid = '". App::getUser('id') ."'");
	if ( !$iCnt )
	{
		$oDb->insert(App::getT('msg_online'), array('userid' => App::getUser('id'), 'time' => time()));
	}

	$aOptions = App::getOptions('im_status');
	$aStatus = array();
	$sCurrent = (isset($aOptions[App::getUser('im_status')]) ? $aOptions[App::getUser('im_status')] : '');
	foreach( $aOptions as $iKey => $sValue )
	{
		$aStatus[] = array('key' => $iKey, 'name' => $sValue);
	}
	
	$sJavaScript = '
		function confirmExit()
		{
			xajax_deleteSessions();
			closeChildWindows();
		}
		window.onunload = confirmExit;
		function playsound(fn) 
		{
			var d = document.getElementById(fn);
			d.innerHTML = \'<embed src="'. App::getParam('url') .'file/player/beep.wav" autostart="true" hidden="true" volumn="100" look="false" />\';
		}';

	$oTpl->assign(array
	(
		'sUser' => App::getUser('user'),
		'bImSetting' => App::getUser('msg_event'),
		'aOptions' => $aStatus,
		'sCurrent' => $sCurrent,
		'sExtraHeader' => "<script type=\"text/javascript\">". $sJavaScript ."\n\t\twindow.onload=clicktoHideAll;\n\t\tvar sDisplayIds = new Array('search_box', 'user_status', 'user_setting');\n\t</script>"
	));

	$oTpl->setTemplateFile('webmessenger.html');
}
?>